﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FileRenameToolsV2
{
    /// <summary>
    /// NumberUpDown.xaml 的交互逻辑
    /// </summary>
    public partial class NumberUpDown : UserControl
    {// 依赖属性，用于绑定
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(int), typeof(NumberUpDown),
                new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.Register("Minimum", typeof(int), typeof(NumberUpDown),
                new FrameworkPropertyMetadata(0));

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.Register("Maximum", typeof(int), typeof(NumberUpDown),
                new FrameworkPropertyMetadata(65535));

        public static readonly DependencyProperty IncrementProperty =
            DependencyProperty.Register("Increment", typeof(int), typeof(NumberUpDown),
                new FrameworkPropertyMetadata(1));

        // 属性封装
        public int Value
        {
            get { return (int)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public int Minimum
        {
            get { return (int)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }

        public int Maximum
        {
            get { return (int)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        public int Increment
        {
            get { return (int)GetValue(IncrementProperty); }
            set { SetValue(IncrementProperty, value); }
        }

        public NumberUpDown()
        {
            InitializeComponent();
            numberValue.DataContext = this;
            numberValue.SetBinding(TextBlock.TextProperty, "Value");
        }

        /// <summary>
        /// 数值发生变化事件
        /// </summary>
        public event RoutedEventHandler ValueChanged;

        private void btnNumberUp_Click(object sender, RoutedEventArgs e)
        {
            if (Value < Maximum)
            {
                Value += Increment;
                numberValue.Text= Value.ToString();
                ValueChanged?.Invoke(this, e);
            }
        }

        private void btnNumberDown_Click(object sender, RoutedEventArgs e)
        {
            if (Value > Minimum)
            {
                Value -= Increment;
                numberValue.Text = Value.ToString();
                ValueChanged?.Invoke(this, e);
            }
        }

        private void numberValue_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            int iValue = 0;
            e.Handled = !int.TryParse(e.Text, out iValue);
        }

        private void numberValue_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!int.TryParse(numberValue.Text, out int value))
            {
                Value = Minimum;
            }
            else
            {
                Value = Math.Clamp(value, Minimum, Maximum);
            }
            numberValue.Text = Value.ToString();
            ValueChanged?.Invoke(this, e);
        }
    }
}
